<?php
class Questions_Model extends grocery_CRUD_Model
{
	public function get_chapter_name($question_id){
		$this->db->where('id',$question_id);
		$rs = $this->db->get('questions');
		$rs = $rs->result_array();
		return $rs[0]['chapter_name'];
	}
	
	public function get_qty($question_id){
		$this->db->where('id',$question_id);
		$rs = $this->db->get('questions');
		$rs = $rs->result_array();
		return $rs[0]['qty'];
	}
	
    function get_list()
    {
    	if($this->table_name === null)
    		return false;
    	
    	$select = "DISTINCT {$this->table_name}.*, COUNT(questions_detail.question_id) as jumlah_sekarang";
    	
		// ADD YOUR SELECT FROM JOIN HERE <------------------------------------------------------
		//$select .= ", user_log.created_date, user_log.update_date";
		
    	if(!empty($this->relation))
    		foreach($this->relation as $relation)
    		{
    			list($field_name , $related_table , $related_field_title) = $relation;
    			$unique_join_name = $this->_unique_join_name($field_name);
    			$unique_field_name = $this->_unique_field_name($field_name);
    			
				if(strstr($related_field_title,'{'))
    				$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
    			else    			
    				$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
    			
    			if($this->field_exists($related_field_title))
    				$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
    		}
    		
    	$this->db->select($select, false);
    	
		// ADD YOUR JOIN HERE for example: <------------------------------------------------------
		$this->db->join('questions_detail','question_id = questions.id','left');
		$this->db->where('teacher_id',$_SESSION['person_id']);
		$this->db->where('subject_id',$_SESSION['subject_id']);
		$this->db->group_by(array("id")); 
    	$results = $this->db->get($this->table_name)->result();
		//echo $this->db->last_query();
    	return $results;
    }
	
	function get_edit_values($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
		$_SESSION['question_id'] = $primary_key_value;
    	$this->db->where($primary_key_field,$primary_key_value);
    	$result = $this->db->get($this->table_name)->row();
    	return $result;
    }

	function db_update($post_array, $primary_key_value)
    {
		/*
		$CI =& get_instance();
		$CI->load->model('questions_detail_model');
		$questions_detail_qty = $CI->questions_detail_model->get_qty($primary_key_value);
		
		if($post_array['qty'] < $questions_detail_qty){
			echo "ERROR: Jumlah total soal dalam BAB tidak boleh < total soal yang sudah ada didalamnya";
			return false;
		}
		else{		
			$primary_key_field = $this->get_primary_key();
			return $this->db->update($this->table_name,$post_array, array( $primary_key_field => $primary_key_value));
		}
		*/
		$primary_key_field = $this->get_primary_key();
		return $this->db->update($this->table_name,$post_array, array( $primary_key_field => $primary_key_value));
    }

	function db_insert($post_array)
    {
		$post_array['subject_id'] = $_SESSION['subject_id'];
		$insert = $this->db->insert($this->table_name,$post_array);
    	if($insert)
    	{
    		return $this->db->insert_id();
    	}
    	return false;
    }
	
	function db_delete($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	
    	if($primary_key_field === false)
    		return false;
    	
    	$this->db->limit(1);
    	$this->db->delete($this->table_name,array( $primary_key_field => $primary_key_value));
    	if( $this->db->affected_rows() != 1)
    		return false;
    	else
    		return true;
    }
	
}